package com.GDOU.controller;

import com.GDOU.bean.Student;
import com.GDOU.bean.TLeave;
import com.GDOU.service.LeaveInfoService;
import com.GDOU.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/leave")
public class LeaveInfoAction {

    @Autowired
    LeaveInfoService leaveInfoService;

    //@ResponseBody
    @RequestMapping("/ajaxSplit")
    public String ajaxSplit(Integer stuId,Integer typeId,Integer statuId,HttpServletRequest request){
        List<TLeave> leaveInfoList = leaveInfoService.ajaxSplit(stuId,typeId,statuId);
        request.getSession().setAttribute("leaveInfoList",leaveInfoList);
        return "forward:/leave/split.action";
    }



    @ResponseBody
    @RequestMapping(value = "/delete",produces = "text/html;charset=UTF-8")
    public Object delete(int id,HttpServletRequest request){
        int count = leaveInfoService.delete(id);
        if(count > 0){

            //注释打开，导入新的学生类！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！

            Student tStu = (Student) request.getSession().getAttribute("tStu");
            Integer stuId = tStu.getStuId();
            List<TLeave> leaveInfoList = leaveInfoService.spilt(stuId);
            request.getSession().setAttribute("leaveInfoList",leaveInfoList);
            return "删除成功";
        }
        else {
            return "删除失败";
        }
    }


    @RequestMapping("/update")
    public String update(TLeave tLeave,HttpServletRequest request){
        String beginTime = tLeave.getBeginTime();
        if(beginTime == ""){
            request.setAttribute("msg","续假信息有误，请重新输入");
            return "forward:/leave/split.action";
        }
        String endTime = tLeave.getEndTime();
        if(endTime == ""){
            request.setAttribute("msg","续假信息有误，请重新输入");
            return "forward:/leave/split.action";
        }
        int totalDays = DateUtil.daysBetween(beginTime, endTime);
        if(totalDays < 0){
            request.setAttribute("msg","续假信息有误，请重新输入");
            return "forward:/leave/split.action";
        }
        tLeave.setDayNum(totalDays);
        tLeave.setStatuId(1);

        int count = leaveInfoService.update(tLeave);
        if(count > 0){
            request.setAttribute("msg","续假申请已发送");
        }else{
            request.setAttribute("msg","续假申请信息有误，请重新输入");
        }
        return "forward:/leave/split.action";
    }


    @RequestMapping("/one")
    public String updateDisplay(int id,HttpServletRequest request){
        TLeave tLeave = leaveInfoService.getBeginAndEndTime(id);
        request.setAttribute("tLeave",tLeave);
        return "update";
    }

    @RequestMapping("split")
    public String split(HttpServletRequest request){

        //注释打开，导入新的学生类！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！

        Student tStu = (Student) request.getSession().getAttribute("tStu");
        Integer stuId = tStu.getStuId();
        List<TLeave> list = leaveInfoService.spilt(stuId);
        request.setAttribute("leaveInfoList",list);

        return "leaveInfo";
    }

    @RequestMapping("/save")
    public String save(TLeave tLeave,HttpServletRequest request){
        String beginTime = tLeave.getBeginTime();
        String endTime = tLeave.getEndTime();
        if(beginTime == ""){
            request.setAttribute("msg","请假信息不完整，申请发送失败");
            return "forward:/leave/split.action";
        }
        if(endTime == ""){
            request.setAttribute("msg","请假信息不完整，申请发送失败");
            return "forward:/leave/split.action";
        }
        int totalDay = DateUtil.daysBetween(beginTime, endTime);
        if(totalDay < 0){
            request.setAttribute("msg","请假信息错误，申请发送失败");
            return "forward:/leave/split.action";
        }
        tLeave.setDayNum(totalDay);
        tLeave.setStatuId(1);
        int count = -1;
        count = leaveInfoService.save(tLeave);
        if(count > 0){
            request.setAttribute("msg","请假申请已发送");
        }else{
            request.setAttribute("msg","请假申请发送失败");
        }
        return "forward:/leave/split.action";
    }

}
